import { defineStore } from 'pinia';
import curdUtils from "/@/stores/ums/customer/curdUtils";
import {customerFieldStateApi as api} from "/@/api/ums/customer/fieldStateApi";
import {ElMessage} from "element-plus";
import { statusStageHistoryApi } from "/@/api/ums/customer/statusStageHistoryApi";

/**
 * 阶段状态 - 全局缓存
 */
export const stageStatusCache = defineStore('stageStatus', {
	state: ():any => ({
		// 行为行动记录统计列表
		list: [],
		// 行为行动记录统计列表  - get 加载的锁
		lock: false,
		// 类型
		type: 1,
		// 标题
		title: '阶段状态',
		statusList:[],// 统计数据列表 - 后续可以单独刷新.
		statusListLoading: false,//  统计数据列表加载状态控制
	}),

	/** 计算属性 */
	getters:{

		/** 计算 - 状态列表为  */
		stagStatusCreate: state =>{
			// 过滤分组 和 不显示的
			return state.list
				.filter((sl: any) => (sl['visible'] === 0 && !groupTable.has(sl['id'])))
				.filter((sl: any) => createPage.has(sl['name']));
		},

		/** 计算 状态列表 */
		stageStatusList: state =>{
			// 回填数据
			return state.list
				.filter((sl: any) => (sl['visible'] === 0 && !groupTable.has(sl['id'])))
				.map((sl: any) => {
					const i = state.statusList.findIndex((fi: any) => fi.stageId === sl.id)
					if (i === -1) {
						sl['current'] = 0
						sl['today'] = 0
						sl['yesterday'] = 0
					} else {
						sl['current'] = state.statusList[i].current
						sl['today'] = state.statusList[i].today
						sl['yesterday'] = state.statusList[i].yesterday
					}
					return sl
				})
		},

		/** 计算 废弃状态 */
		disuseStatusList: state =>{
			// 过滤分组 和 不显示的
			return state.list
				.filter((sl: any) => (sl['visible'] === 0 && groupTable.has(sl['id'])))
				.map((sl:any)=>{
					const i = state.statusList.findIndex((fi: any) => fi.stageId === sl.id)
					if (i === -1) {
						sl['current'] = 0
						sl['today'] = 0
						sl['yesterday'] = 0
					} else {
						sl['current'] = state.statusList[i].current
						sl['today'] = state.statusList[i].today
						sl['yesterday'] = state.statusList[i].yesterday
					}
					return sl
				})
		},

	},

	actions: {

		/** 获取 - 阶段状态 */
		getList(){
			// 检查是否有数据
			if (this.isLoad(this.lock, this.list)) {
				this.lock = true
				this.loadList()
			}
			return this.list
		},

		/** 加载 - 阶段状态 */
		async loadList(){
			try{
				const params = {'type': this.type};
				const {data} = await api().query(params);
				// 清除重新加载
				this.checkReset(this.list)
				// 增量加载 - 直接赋值会导致引用失效
				this.list.push(...data);
				// 针对需要小时隐藏的数据进行重新
				this.loadStatusList()
			}catch (e:any){
				console.log('获取'+this.title+'列表异常捕获\n',e);
				ElMessage.error(e.message)
			}
			this.lock = false
		},

		/** 加载阶段列表统计 */
		async loadStatusList() {
			if (!this.list) return
			this.statusListLoading = true
			try {
				const ids = this.list.filter((sl: any) => sl['visible'] === 0).map((m:any) => m.id)
				const {data} = await statusStageHistoryApi().stats(ids)
				this.checkReset(this.statusList)
				this.statusList.push(...data)
			}catch (e) {
				console.log("加载统计数据\n",e);
			}
			this.statusListLoading = false
		},

		/** 批量修改显示或隐藏 - 阶段状态  */
		async batchVisibleByIds(ids:any){
			await this.batchVisible(ids, this.type, api, this.loadList, this.title)
		},

		init(){
			if (this.isLoad(this.lock, this.list)) {
				this.lock = true
				this.loadList()
			}else {
				this.loadStatusList()
			}
		},

		// 公共方法
		...curdUtils
	},

	persist: {
		// 开启持久化
		enabled: true,
		strategies: [
			{
				key: '17-work-stageStatusCache',
				storage: sessionStorage, // 仅会话期间有效
				paths: ['list']
			},
			// {storage: localStorage, paths: ['']},
		],
	},

});


const groupTable = new Map();
groupTable.set("10", true)
groupTable.set("11", true)
groupTable.set("12", true)

const createPage = new Map()
createPage.set("线索", true)
createPage.set("机会", true)
createPage.set("方案", true)
createPage.set("谈判", true)
createPage.set("刚签约", true)
createPage.set("交付回款", true)